<?php

// 数据库配置
session_start();

if (!isset($_SESSION['username'])) {
    header("Location: ../login.php");
    exit();
}

include 'config.php'; // 确保 config.php 包含数据库连接 $conn

// 检查是否是 POST 请求
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // 从请求体中读取 JSON 数据
    $json = file_get_contents('php://input');
    $data = json_decode($json, true);

    // 获取 QQ 列表
    $qqList = $data['qqList'] ?? [];

    // 打印调试信息
    error_log(print_r($qqList, true));

    // 检查是否提供了有效的 QQ 列表
    if (!empty($qqList)) {
        // 准备 SQL 语句
        $placeholders = implode(',', array_fill(0, count($qqList), '?')); // 创建占位符列表
        $deleteStmt = $conn->prepare("DELETE FROM sq WHERE qq IN ($placeholders)");

        // 绑定参数
        $types = str_repeat('s', count($qqList)); // 绑定类型为字符串
        $deleteStmt->bind_param($types, ...$qqList);

        if ($deleteStmt->execute()) {
            $response = [
                "code" => "200",
                "msg" => "删除成功"
            ];
        } else {
            $response = [
                "code" => "500",
                "msg" => "删除失败",
                "error" => $deleteStmt->error
            ];
        }
    } else {
        $response = [
            "code" => "400",
            "msg" => "未提供有效的QQ列表"
        ];
    }

    // 返回 JSON 响应
    echo json_encode($response, JSON_UNESCAPED_UNICODE);
} else {
    // 如果不是 POST 请求，返回错误
    header("HTTP/1.1 405 Method Not Allowed");
    exit("仅支持 POST 请求");
}

// 关闭数据库连接
$conn->close();
?>
